package com.topisv.tms.webservice.soap.sf;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sf.integration.expressservice.service.IService;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.utils.sf.MailSFUtil;
import com.topisv.tms.webservice.soap.sf.vo.CreateSFVO;
import com.topisv.tms.webservice.soap.sf.vo.OrdenoRouteSFVO;
import com.topisv.tms.webservice.soap.sf.vo.RouteSFVO;

@Component
public class IserviceImpl {
    
    private static final Logger log = LoggerFactory.getLogger(IserviceImpl.class);

	@Autowired
	private IService iService;


	/**
	 * 下单接口
	 * @param e
	 * @return
	 */
	public CreateSFVO createOrder(EdiOrder e) {
		log.info("==================开始创建订单="+e.getOrdeno()+"==================");
		CreateSFVO createSFVO=new CreateSFVO();
		//String xml = MailSFUtil.createOrdeno(e);
		//String result = iService.sfexpressService(xml);
		/*try {
			Document document = DocumentHelper.parseText(result);
			Element rootElement = document.getRootElement();
			String serviceName = rootElement.attributeValue("service");
			createSFVO.setServiceName(serviceName);
			log.info("服务名称=" + serviceName);
			Element headElement = rootElement.element("Head");
			String headText = headElement.getText();
			createSFVO.setHeadText(headText);
			log.info("返回状态=" + headText);
			if ("OK".equals(headText)) {
				Element bodyElement = rootElement.element("Body");

				Element responseElement = bodyElement.element("OrderResponse");
				String filter_result = responseElement.attributeValue("filter_result");
				createSFVO.setFilterResult(filter_result);
				e.setFilterResult(filter_result);
				log.info("filter_result=" + filter_result);
				String destcode = responseElement.attributeValue("destcode");
				createSFVO.setDestcode(destcode);
				log.info("destcode=" + destcode);
				e.setDestCode(destcode);
				String mailno = responseElement.attributeValue("mailno");
				createSFVO.setMailno(mailno);
				log.info("mailno=" + mailno);
				e.setMailNo(mailno);
				String origincode = responseElement.attributeValue("origincode");
				createSFVO.setOrigincode(origincode);
				log.info("origincode=" + origincode);
				e.setOriginCode(origincode);
				String orderid = responseElement.attributeValue("orderid");
				createSFVO.setOrderid(orderid);
				log.info("orderid=" + orderid);
			} else if ("ERR".equals(headText)) {
				Element errorElement = rootElement.element("ERROR");
				String code = errorElement.attributeValue("code");
				createSFVO.setCode(code);
				log.info("code=" + code);
				createSFVO.setErrorText(errorElement.getText());
				log.info(errorElement.getText());
			}

		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			
			return null;
		}
*/
		//log.info(result);
		return createSFVO;
	}
	
	public String createOrder2(EdiOrder e) {
		log.info("==================开始创建订单="+e.getOrdeno()+"==================");
		CreateSFVO createSFVO=new CreateSFVO();
		//String xml = MailSFUtil.createOrdeno(e);
		return "";
	}

	/**
	 * 取消订单接口
	 * @param e
	 * @return
	 */
	public CreateSFVO confirmCancelOrdeno(EdiOrder e) {
		log.info("============开始取消订单："+e.getOrdeno()+"="+e.getMailNo()+"=============");
		String xml = MailSFUtil.confirmCancelOrdeno(e);
		CreateSFVO createSFVO=new CreateSFVO();
		String result = iService.sfexpressService(xml);
		Document document = null;
		try {
			document = DocumentHelper.parseText(result);
			Element rootElement = document.getRootElement();
			String serviceName = rootElement.attributeValue("service");
			log.info("服务名称=" + serviceName);
			createSFVO.setServiceName(serviceName);
			Element headElement = rootElement.element("Head");
			String headText = headElement.getText();
			createSFVO.setHeadText(headText);
			log.info("返回状态=" + headText);
			if ("OK".equals(headText)) {
				Element bodyElement = rootElement.element("Body");

				Element responseElement = bodyElement.element("OrderConfirmResponse");
				String orderid = responseElement.attributeValue("orderid");
				createSFVO.setOrderid(orderid);
				log.info("orderid=" + orderid);
				String remark = responseElement.attributeValue("remark");
				createSFVO.setRemark(remark);
				log.info("remark=" + remark);
				String mailno = responseElement.attributeValue("mailno");
				createSFVO.setMailno(mailno);
				log.info("mailno=" + mailno);
			} else if ("ERR".equals(headText)) {
				Element errorElement = rootElement.element("ERROR");
				
				String code = errorElement.attributeValue("code");
				createSFVO.setCode(code);
				log.info("code=" + code);
				createSFVO.setErrorText(errorElement.getText());
				log.info(errorElement.getText());
			}
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			
			return null;
		}
		
		return createSFVO;

	}

	/**
	 * 查询订单接口，用户调用下单接口没有返回值后的处理方式
	 * @param ordeno
	 * @return
	 */
	public CreateSFVO searchOrdeno(String ordeno) {
		log.info("==================开始查询订单="+ordeno+"==================");
		String xml = MailSFUtil.searchOrdeno(ordeno);
		String result = iService.sfexpressService(xml);
		CreateSFVO createSFVO=new CreateSFVO();
		Document document = null;
		try {
			document = DocumentHelper.parseText(result);
			Element rootElement = document.getRootElement();
			String serviceName = rootElement.attributeValue("service");
			log.info("服务名称=" + serviceName);
			createSFVO.setServiceName(serviceName);
			Element headElement = rootElement.element("Head");
			String headText = headElement.getText();
			log.info("返回状态=" + headText);
			createSFVO.setHeadText(headText);
			if ("OK".equals(headText)) {
				Element bodyElement = rootElement.element("Body");

				Element responseElement = bodyElement.element("OrderResponse");
				String orderid = responseElement.attributeValue("orderid");
				log.info("orderid=" + orderid);
				createSFVO.setOrderid(orderid);
				String remark = responseElement.attributeValue("remark");
				log.info("remark=" + remark);
				createSFVO.setRemark(remark);
				String mailno = responseElement.attributeValue("mailno");
				log.info("mailno=" + mailno);
				createSFVO.setMailno(mailno);
				String origincode = responseElement.attributeValue("origincode");
				log.info("origincode=" + origincode);
				createSFVO.setOrigincode(origincode);
				String destcode = responseElement.attributeValue("destcode");
				log.info("destcode=" + destcode);
				createSFVO.setDestcode(destcode);
				String filter_result = responseElement.attributeValue("filter_result");
				log.info("filter_result==" + filter_result);
				createSFVO.setFilterResult(filter_result);
			} else if ("ERR".equals(headText)) {
                 Element errorElement = rootElement.element("ERROR");
				
				String code = errorElement.attributeValue("code");
				createSFVO.setCode(code);
				log.info("code=" + code);
				createSFVO.setErrorText(errorElement.getText());
				log.info(errorElement.getText());
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			
			return null;
		}
		return createSFVO;
	}

	/**
	 * 筛单接口，现在不使用
	 * @param e
	 * @return
	 */
	public CreateSFVO filterOrdeno(EdiOrder e) {
		log.info("==================开始筛选订单="+e.getOrdeno()+"===============");
		String xml = MailSFUtil.filterOrdeno(e);
		String result = iService.sfexpressService(xml);
		CreateSFVO createSFVO=new CreateSFVO();
		Document document = null;
		try {
			document = DocumentHelper.parseText(result);
			Element rootElement = document.getRootElement();
			String serviceName = rootElement.attributeValue("service");
			log.info("服务名称=" + serviceName);
			createSFVO.setServiceName(serviceName);
			Element headElement = rootElement.element("Head");
			String headText = headElement.getText();
			log.info("返回状态=" + headText);
			createSFVO.setHeadText(headText);
			if ("OK".equals(headText)) {
				Element bodyElement = rootElement.element("Body");

				Element responseElement = bodyElement.element("OrderResponse");
				String orderid = responseElement.attributeValue("orderid");
				log.info("orderid=" + orderid);
				createSFVO.setOrderid(orderid);
				String remark = responseElement.attributeValue("remark");
				log.info("remark=" + remark);
				createSFVO.setRemark(remark);
				String origincode = responseElement.attributeValue("origincode");
				log.info("origincode=" + origincode);
				createSFVO.setOrigincode(origincode);
				String destcode = responseElement.attributeValue("destcode");
				log.info("destcode=" + destcode);
				createSFVO.setDestcode(destcode);
				String filter_result = responseElement.attributeValue("filter_result");// 1=人工确认，2：可派送，3：不可派送
				log.info("filter_result==" + filter_result);
				createSFVO.setFilterResult(filter_result);
			} else if ("ERR".equals(headText)) {
                Element errorElement = rootElement.element("ERROR");
				
				String code = errorElement.attributeValue("code");
				createSFVO.setCode(code);
				log.info("code=" + code);
				createSFVO.setErrorText(errorElement.getText());
				log.info(errorElement.getText());
			}
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			
			return null;
		}
		return createSFVO;
	}

	/**
	 * 订单流转详情查询接口
	 * @param ordeno
	 * @return
	 */
	public CreateSFVO routeOrdeno(String ordeno) {
		log.info("===================订单路由查询="+ordeno+"============");
		String xml = MailSFUtil.routeOrdeno(ordeno);
		String result = iService.sfexpressService(xml);
		CreateSFVO createSFVO=new CreateSFVO();
		Document document = null;
		try {
			document = DocumentHelper.parseText(result);
			Element rootElement = document.getRootElement();
			String serviceName = rootElement.attributeValue("service");
			log.info("服务名称=" + serviceName);
			createSFVO.setServiceName(serviceName);
			Element headElement = rootElement.element("Head");
			String headText = headElement.getText();
			log.info("返回状态=" + headText);
			createSFVO.setHeadText(headText);
			if ("OK".equals(headText)) {
				Element bodyElement = rootElement.element("Body");
				List<Element> responseElements = bodyElement.elements("RouteResponse");
				List<OrdenoRouteSFVO> os=new ArrayList<OrdenoRouteSFVO>();
				for (Element responseElement : responseElements) {
					OrdenoRouteSFVO o=new OrdenoRouteSFVO();
					String orderid = responseElement.attributeValue("orderid");
					log.info("orderid=" + orderid);
					o.setOrderid(orderid);
					String mailno = responseElement.attributeValue("mailno");
					log.info("mailno=" + mailno);
					o.setMailno(mailno);
					List<RouteSFVO> rs=new ArrayList<RouteSFVO>();
					List<Element> routeElements = responseElement.elements("Route");
					for (Element routeElement : routeElements) {
						RouteSFVO r=new RouteSFVO();
						String accept_time = routeElement.attributeValue("accept_time");
						log.info("accept_time=" + accept_time);
						r.setAcceptTime(accept_time);
						String accept_address = routeElement.attributeValue("accept_address");
						log.info("accept_address=" + accept_address);
						r.setAcceptAddress(accept_address);
						String remark = routeElement.attributeValue("remark");
						log.info("remark=" + remark);
						r.setRemark(remark);
						String opCode = routeElement.attributeValue("opCode");
						log.info("opCode=" + opCode);
						r.setOpCode(opCode);
						rs.add(r);
					}
					o.setRouteSFVOs(rs);
				}
				createSFVO.setOrdenoRouteSFVO(os);
			} else if ("ERR".equals(headText)) {
                Element errorElement = rootElement.element("ERROR");
				
				String code = errorElement.attributeValue("code");
				createSFVO.setCode(code);
				log.info("code=" + code);
				createSFVO.setErrorText(errorElement.getText());
				log.info(errorElement.getText());
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			
			return null;
		}
		return createSFVO;
	}

	/**
	 * 路由推送信息处理
	 * @param xml
	 * @return
	 */
	public List<RouteSFVO> routePush(String xml) {
		Document document = null;
		List<RouteSFVO> routeSFVOs=new ArrayList<RouteSFVO>();
		try {
			document = DocumentHelper.parseText(xml);
			Element rootElement = document.getRootElement();
			String serviceName = rootElement.attributeValue("service");
			log.info("服务名称=" + serviceName);
			String lang = rootElement.attributeValue("lang");
			log.info("lang=" + lang);
			Element bodyElement = rootElement.element("Body");
			RouteSFVO routeSFVO=new RouteSFVO();
			List<Element> waybillElements = bodyElement.elements("WaybillRoute");//最多十个
			for (Element waybillElement : waybillElements) {
				String orderid = waybillElement.attributeValue("orderid");//订单编码
				log.info("orderid=" + orderid);
				routeSFVO.setOrderid(orderid);
				String mailno = waybillElement.attributeValue("mailno");//运单编号
				log.info("mailno=" + mailno);
				routeSFVO.setMailno(mailno);
				String id = waybillElement.attributeValue("id");//路由ID
				log.info("id=" + id);
				routeSFVO.setId(id);
				String acceptTime = waybillElement.attributeValue("acceptTime");// 路由发生时间
				log.info("acceptTime=" + acceptTime);
				routeSFVO.setAcceptTime(acceptTime);
				String acceptAddress = waybillElement.attributeValue("acceptAddress");// 路由发生地点
				log.info("acceptAddress=" + acceptAddress);
				routeSFVO.setAcceptAddress(acceptAddress);
				String remark = waybillElement.attributeValue("remark");// 详细说明
				log.info("remark=" + remark);
				routeSFVO.setRemark(remark);
				String opCode = waybillElement.attributeValue("opCode");// 操作码
				log.info("opCode=" + opCode);
				routeSFVO.setOpCode(opCode);
				routeSFVOs.add(routeSFVO);
			}

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			
			return null;
		}
		return routeSFVOs;
	}
	
	
}
